# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=bespokesynth
pkgver=1.2.1
_commit=9a2072ed509e23bc5ead29fe59a3897aa137d2ec  # refs/tags/v1.2.1
pkgrel=1
pkgdesc="A software modular synth"
arch=(loong64 x86_64)
url="https://www.bespokesynth.com/"
license=(GPL-3.0-or-later)
groups=(pro-audio)
depends=(
  gcc-libs
  glibc
  hicolor-icon-theme
  libglvnd
  libpng
  python
  zlib
)
makedepends=(
  abletonlink
  alsa-lib
  cmake
  flac
  freetype2
  git
  jack
  jsoncpp
  juce
  libjpeg-turbo
  libogg
  libvorbis
  libusb
  libx11
  libxcursor
  libxinerama
  pybind11
  tuning-library
  xorg-xrandr
)
optdepends=(
  'jack: to use with JACK'
  'python-jedi: for code completion'
)
provides=(vst3-host)
source=(
  $pkgname::git+https://github.com/$pkgname/$pkgname#commit=$_commit
  MTS-ESP::git+https://github.com/ODDSound/MTS-ESP
  $pkgname-1.2.0-devendor_abletonlink.patch
)
sha512sums=('SKIP'
            'SKIP'
            '02a78c539f990aa3662211ea2da7afd65cdb0b8e39d723f3083970bf591c6dcdbc2a8be3b79df61e8d8c6bdfa22b56af15496ae83484428cfaf56e7f0857a147')
b2sums=('SKIP'
        'SKIP'
        'e70e2d63535cd08ec6bb55054840913ac45c600c642a343e1e852c46e453c0adb630362e253030a8965c2f433752c88b6a1f7283abd40c0d5ee72eb31cf108c1')

pkgver() {
  cd $pkgname
  git describe --exclude Nightly --tags | sed 's/v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd $pkgname
  git submodule init
  git config submodule.libs/oddsound-mts/MTS-ESP.url "$srcdir/MTS-ESP"
  git submodule deinit libs/JUCE
  git submodule deinit libs/pybind11
  git submodule deinit libs/tuning-library
  git submodule deinit libs/json/jsoncpp
  git -c protocol.file.allow=always submodule update

  # devendor abletonlink: https://github.com/BespokeSynth/BespokeSynth/pull/1397
  patch -Np1 -i ../$pkgname-1.2.0-devendor_abletonlink.patch
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D BESPOKE_DEVENDORED_SYSTEM_JUCE=ON
    -D BESPOKE_SYSTEM_ABLETONLINK=ON
    -D BESPOKE_SYSTEM_PYBIND11=ON
    -D BESPOKE_SYSTEM_JSONCPP=ON
    -D BESPOKE_SYSTEM_TUNING_LIBRARY=ON
    -D BESPOKE_SYSTEM_JUCE=ON
    -S $pkgname
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  ctest --test-dir build --output-on-failure
}

package() {
  depends+=(
    alsa-lib libasound.so
    flac libFLAC.so
    freetype2 libfreetype.so
    jsoncpp libjsoncpp.so
    libjpeg-turbo libjpeg.so
    libogg libogg.so
    libvorbis libvorbis.so
    libusb libusb-1.0.so
  )

  DESTDIR="$pkgdir" cmake --install build
}
